package com.sunbeam.common.junit;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.ArrayList;
import com.sunbeam.common.junit.accessories.UserInfoEntity;


public class InvokeTest {

/**
 * 
 * @Title: main
 * @Description: TODO
 * @param @param args
 * @param @throws IllegalAccessException
 * @param @throws IllegalArgumentException
 * @param @throws InvocationTargetException
 * @return void
 * @throws
 */
	public static void main(String[] args) throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException {
		// TODO Auto-generated method stub
		UserInfoEntity userInfo = new UserInfoEntity();
		ArrayList<UserInfoEntity> userInfos = new ArrayList<UserInfoEntity>();
		// printMember(userInfo);
		// printConstructors(userInfo);
		// printMethods(userInfo);
		Class userInfoClazz = userInfo.getClass();
		Field[] fields = userInfoClazz.getDeclaredFields();
		Method[] methods = userInfoClazz.getMethods();

		for (int i = 0; i < 200000; i++) {
			userInfo = new UserInfoEntity();
			for (int j = 0; j < fields.length; j++) {
				for (int k = 0; k < methods.length; k++) {
					if (methods[k].getName().equals("set" + fields[j].getName())) {
						if (fields[j].getType().equals(String.class)) {
							methods[k].invoke(userInfo, fields[j].getName() + (i+1));
						} else {
							methods[k].invoke(userInfo, new Date(System.currentTimeMillis()-(long)(Math.random()*999999999999L)));
						}
					}
				}
			}
			userInfos.add(userInfo);
		}
		
		long l1=System.currentTimeMillis();
		for (UserInfoEntity userInfoEntity : userInfos) {
			for (int j = 0; j < fields.length; j++) {
				for (int k = 0; k < methods.length; k++) {
					if (methods[k].getName().equals("get" + fields[j].getName())) {
						System.out.println(methods[k].getName() + ":\t"+ methods[k].invoke(userInfoEntity));
					}
				}
			}
		}
		long l2=System.currentTimeMillis();
		
		long l3=System.currentTimeMillis();
		for (int i=0;i< userInfos.size();i++) {
			for (int j = 0; j < fields.length; j++) {
				for (int k = 0; k < methods.length; k++) {
					if (methods[k].getName().equals("get" + fields[j].getName())) {
						System.out.println(methods[k].getName() + ":\t" + methods[k].invoke(userInfos.get(i)));
					}
				}
			}
		}
		long l4=System.currentTimeMillis();
		
		long l5=System.currentTimeMillis();
		for (int i=0;i< userInfos.size();i++) {
			System.out.println("getUid():\t" +userInfos.get(i).getUID());
			System.out.println("getUserName():\t" +userInfos.get(i).getUserName());
			System.out.println("getPassword():\t" +userInfos.get(i).getPassword());
			System.out.println("getEmail():\t" +userInfos.get(i).getEmail());
			System.out.println("getBirthday():\t" +userInfos.get(i).getBirthday());
		}
		long l6=System.currentTimeMillis();
		
		long l7=System.currentTimeMillis();
		for (UserInfoEntity userInfoEntity : userInfos) {
			System.out.println("getUid():\t" +userInfoEntity.getUID());
			System.out.println("getUserName():\t" +userInfoEntity.getUserName());
			System.out.println("getPassword():\t" +userInfoEntity.getPassword());
			System.out.println("getEmail():\t" +userInfoEntity.getEmail());
			System.out.println("getBirthday():\t" +userInfoEntity.getBirthday());
		}
		long l8=System.currentTimeMillis();
		
		 System.out.println("Invoke+newFor:\t"+(l2-l1)/1000.000+"s");
		 System.out.println("Invoke+oldFor:\t"+(l4-l3)/1000.000+"s");
		 System.out.println("Init+oldFor:\t"+(l6-l5)/1000.000+"s");
		 System.out.println("Init+newFor:\t"+(l8-l7)/1000.000+"s");
	}

	public Object Load(String cName, String MethodName, String[] type,
			String[] param) {
		Object retobj = null;
		try {
			// ����ָ����Java��
			Class cls = Class.forName(cName);

			// ��ȡָ�������ʵ��
			Constructor ct = cls.getConstructor(null);
			Object obj = ct.newInstance(null);

			// ��������������������
			Class partypes[] = this.getMethodClass(type);

			// ��ָ�����л�ȡָ���ķ���
			Method meth = cls.getMethod(MethodName, partypes);

			// ���������Ĳ���ֵ
			Object arglist[] = this.getMethodObject(type, param);

			// ����ָ���ķ�������ȡ����ֵΪObject����
			retobj = meth.invoke(obj, arglist);

		} catch (Throwable e) {
			System.err.println(e);
		}
		return retobj;
	}

	// ��ȡ��������Class[]�ķ���
	public Class[] getMethodClass(String[] type) {
		Class[] cs = new Class[type.length];
		for (int i = 0; i < cs.length; i++) {
			if (!type[i].trim().equals("") || type[i] != null) {
				if (type[i].equals("int") || type[i].equals("Integer")) {
					cs[i] = Integer.TYPE;
				} else if (type[i].equals("float") || type[i].equals("Float")) {
					cs[i] = Float.TYPE;
				} else if (type[i].equals("double") || type[i].equals("Double")) {
					cs[i] = Double.TYPE;
				} else if (type[i].equals("boolean")
						|| type[i].equals("Boolean")) {
					cs[i] = Boolean.TYPE;
				} else {
					cs[i] = String.class;
				}
			}
		}
		return cs;
	}

	// ��ȡ����Object[]�ķ���
	public Object[] getMethodObject(String[] type, String[] param) {
		Object[] obj = new Object[param.length];
		for (int i = 0; i < obj.length; i++) {
			if (!param[i].trim().equals("") || param[i] != null) {
				if (type[i].equals("int") || type[i].equals("Integer")) {
					obj[i] = new Integer(param[i]);
				} else if (type[i].equals("float") || type[i].equals("Float")) {
					obj[i] = new Float(param[i]);
				} else if (type[i].equals("double") || type[i].equals("Double")) {
					obj[i] = new Double(param[i]);
				} else if (type[i].equals("boolean")
						|| type[i].equals("Boolean")) {
					obj[i] = new Boolean(param[i]);
				} else {
					obj[i] = param[i];
				}
			}
		}
		return obj;
	}

	private static void printMember(UserInfoEntity userInfo) {
		Class temp = userInfo.getClass();
		try {
			System.out.println("�����������й��е�����");
			Field[] fb = temp.getFields();
			for (int j = 0; j < fb.length; j++) {
				Class cl = fb[j].getType();
				System.out.println("fb:" + cl);
			}

			System.out.println("�����������е�����");
			Field[] fa = temp.getDeclaredFields();
			for (int j = 0; j < fa.length; j++) {
				Class cl = fa[j].getType();
				System.out.println("fa:" + cl);
			}
			System.out.println("��������˽�����Ե�ֵ");
			Field f = temp.getDeclaredField("Uid");
			f.setAccessible(true);
			Integer i = (Integer) f.get(userInfo);
			System.out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void printConstructors(UserInfoEntity userInfo) {
		Class c = userInfo.getClass();
		// ��ȡָ���������
		String className = c.getName();
		try {
			// ��ȡָ����Ĺ��췽��
			Constructor[] theConstructors = c.getConstructors();
			for (int i = 0; i < theConstructors.length; i++) {
				// ��ȡָ�����췽���Ĳ���ļ���
				Class[] parameterTypes = theConstructors[i].getParameterTypes();

				System.out.print(className + "(");

				for (int j = 0; j < parameterTypes.length; j++)
					System.out.print(parameterTypes[j].getName() + " ");

				System.out.println(")");

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void printMethods(Object o) {
		Class c = o.getClass();
		String className = c.getName();
		Method[] m = c.getMethods();
		for (int i = 0; i < m.length; i++) {
			// ��������ķ�������
			System.out.print(m[i].getReturnType().getName());
			// ���������
			System.out.print(" " + m[i].getName() + "(");
			// ��ȡ�����Ĳ���
			Class[] parameterTypes = m[i].getParameterTypes();
			for (int j = 0; j < parameterTypes.length; j++) {
				System.out.print(parameterTypes[j].getName());
				if (parameterTypes.length > j + 1) {
					System.out.print(",");
				}
			}

			System.out.println(")");
		}

	}
}
